﻿@page "/LongPressButtonTest"
@using BlazorSample;
@inject IJSRuntime JS

<CustomLongPressButton OnClick="HandleClick"
                       OnLongPressStart="HandleLongPressStart"
                       OnLongPressContinued="HandleLongPressContinued"
                       OnLongPressEnd="HandleLongPressEnd"
                       Disabled="@isDisabled"
                       Class="btn btn-primary">
    点击或长按我
</CustomLongPressButton>

<button @onclick="ToggleDisabled" class="btn btn-secondary ml-2">
    切换禁用状态
</button>

<div class="mt-3 alert alert-info">
    <p>当前状态: @(isDisabled ? "禁用" : "启用")</p>
    <p>最后操作: @lastAction</p>
    <p>长按持续时间: @pressDuration.TotalSeconds.ToString("F1") 秒</p>
    <p>持续触发次数: @triggerCount</p>
    <p>长按状态: @(isLongPressing ? "进行中" : "未进行")</p>
</div>


@code {
    private bool isDisabled = false;
    private string lastAction = "无";
    private TimeSpan pressDuration;
    private int triggerCount = 0;
    private bool isLongPressing = false;
    private DateTime? pressStartTime;

    private async Task HandleClick()
    {
        lastAction = "点击";
        pressDuration = TimeSpan.Zero;
        triggerCount = 0;
        StateHasChanged();
    }

    private async Task HandleLongPressStart()
    {
        lastAction = "长按开始";
        pressStartTime = DateTime.Now;
        triggerCount = 1;
        isLongPressing = true;
        StateHasChanged();
    }

    private async Task HandleLongPressContinued()
    {
        if (pressStartTime.HasValue)
        {
            pressDuration = DateTime.Now - pressStartTime.Value;
            triggerCount++;
            lastAction = $"长按持续 (已触发 {triggerCount} 次)";
            StateHasChanged();
        }
    }

    private async Task HandleLongPressEnd()
    {
        if (pressStartTime.HasValue)
        {
            pressDuration = DateTime.Now - pressStartTime.Value;
            lastAction = $"长按结束 (共持续 {pressDuration.TotalSeconds:F1} 秒)";
            isLongPressing = false;
            pressStartTime = null;
            StateHasChanged();
        }
    }

    private void ToggleDisabled()
    {
        isDisabled = !isDisabled;
        lastAction = $"按钮已{(isDisabled ? "禁用" : "启用")}";
    }
}